package com.meilingcloud.web.controller.system;

import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.system.domain.SysSeq;
import com.meilingcloud.system.service.ISysSeqService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 编码规则Controller
 *
 * @author chenl
 * @date 2021-08-25
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/seq")
public class SysSeqController extends BaseController {

    private final ISysSeqService iSysSeqService;

    /**
     * 查询编码规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:seq:list')")
    @GetMapping("/list")
    public AjaxResult list(SysSeq sysSeq) {
        List<SysSeq> list = iSysSeqService.queryList(sysSeq);
        return AjaxResult.success(list);
    }

    /**
     * 获取编码规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:seq:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iSysSeqService.getById(id));
    }

    /**
     * 新增编码规则
     */
    @PreAuthorize("@ss.hasPermi('system:seq:add')")
    @Log(title = "编码规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSeq sysSeq) {
        sysSeq.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSysSeqService.save(sysSeq));
    }

    /**
     * 修改编码规则
     */
    @PreAuthorize("@ss.hasPermi('system:seq:edit')")
    @Log(title = "编码规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSeq sysSeq) {
        sysSeq.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSysSeqService.updateById(sysSeq));
    }

    /**
     * 开启编码规则
     */
    @PreAuthorize("@ss.hasPermi('system:seq:edit')")
    @PostMapping("/active")
    public AjaxResult active(@RequestBody SysSeq sysSeq) {
        sysSeq.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSysSeqService.activeSeq(sysSeq));
    }

    /**
     * 删除编码规则
     */
    @PreAuthorize("@ss.hasPermi('system:seq:remove')")
    @Log(title = "编码规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSysSeqService.removeByIds(Arrays.asList(ids)));
    }
}
